package com.qq5194102.simpleskills;

import com.qq5194102.simpleskills.core.PlayerDataManager;
import com.qq5194102.simpleskills.core.SkillManager;
import com.qq5194102.simpleskills.listener.PlayerJoinListener;
import com.qq5194102.simpleskills.listener.PlayerQuitListener;
import com.qq5194102.simpleskills.model.ExPlayerData;
import com.qq5194102.simpleskills.model.SwordPlayerData;
import com.qq5194102.simpleskills.skill.sword.SwordActiveSkill;
import com.qq5194102.simpleskills.skill.sword.SwordListener;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;

public final class SimpleSkills extends JavaPlugin {

    public SkillManager skillManager;
    // 全局player数据
    private static PlayerDataManager playerDataManager;


    @Override
    public void onEnable() {
        // Plugin startup logic
        skillManager = new SkillManager();
        playerDataManager = new PlayerDataManager(this);
        this.loadGlobalConfig();
        this.registerListeners();
        getLogger().log(Level.INFO,"插件已加载");
    }

    @Override
    public void onDisable() {
        // Plugin shutdown logic
        // TODO pluginManager.saveAllData();
        playerDataManager.onDisable();// 保存玩家数据
        getLogger().log(Level.INFO,"插件已关闭");
    }

    /*
     * 注册监听器
     */
    private void registerListeners() {
        PluginManager pm = getServer().getPluginManager();
        pm.registerEvents(new PlayerJoinListener(), this);
        pm.registerEvents(new PlayerQuitListener(), this);
        pm.registerEvents(new SwordActiveSkill(this), this);
        pm.registerEvents(playerDataManager, this);
        pm.registerEvents(new SwordListener(), this);
        // 删除 skillManager.registerSkillListeners(this);
    }
    /*
     * 加载全局配置
     */
    private void loadGlobalConfig() {
        FileConfiguration config = getConfig();
        // TODO ...
    }

    @Override
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
        getLogger().log(Level.INFO,command.toString());
        getLogger().log(Level.INFO,command.getName().toString());
        getLogger().log(Level.INFO,command.getLabel().toString());
        getLogger().log(Level.INFO, Arrays.toString(args));
        if (command.getName().equalsIgnoreCase("simpleskills")||command.getName().equalsIgnoreCase("skills")||command.getName().equalsIgnoreCase("ss")) {
            if (args.length > 0 && args[0].equalsIgnoreCase("reload")) {
                if (sender.hasPermission("simpleskills.reload")) {
                    // 重载配置文件
                    reloadConfig();
                    // 重载保存间隔
                    playerDataManager.reloadSaveInterval();
                    sender.sendMessage("§aSimpleSkills config配置已重载!");
                    return true;
                }
            }else if (args.length > 1 && args[0].equalsIgnoreCase("info")&&(sender instanceof Player)) {
                switch (args[1]){
                    case "sword":
                        Player player = (Player) sender;
                        ExPlayerData playerData = PlayerDataManager.getPlayerData(player.getUniqueId());
                        SwordPlayerData data = playerData.getSwordSkillData();

                        sender.sendMessage(" §c------  [剑术] ------ ");
                        sender.sendMessage("§f经验值: §6" + data.getExperience());
                        sender.sendMessage("§f距离下一级还需: §6" + data.getHowMuchExpNextLevel() + " §f经验");

                        // 新增：主动技能详情
                        sender.sendMessage(" §c------ 主动:斩击 ------ ");
                        sender.sendMessage("§f伤害: §6" + data.getActiveSkillDamage() + " §c❤");
                        sender.sendMessage("§f攻击距离: §6" + data.getActiveDistance() + " §a格");
                        sender.sendMessage("§f冷却时间: §6" + (data.getActiveColdDown()/1000.0) + " §b秒");

                        sender.sendMessage(" §c------ 被动:暴击 ------ ");
                        sender.sendMessage("§f暴击几率: §6" + PlayerDataManager.getPlayerData(player.getUniqueId()).getSwordSkillData().getCriticalStrikeRate() + "%");
                        sender.sendMessage("§f暴击伤害加成: §6" + PlayerDataManager.getPlayerData(player.getUniqueId()).getSwordSkillData().getCriticalStrikeBonusRate() + "%");
                        break;
                    case "axe":
                        sender.sendMessage("§c开发中");
                        break;
                    default:
                        sender.sendMessage("§c无该技能");
                        break;
                }

            }else{
                // todo
                sender.sendMessage("§6   -----   SimpleSkills 帮助   -----   ");
                sender.sendMessage("§f/skills info [技能]  §a查看技能数据");
                sender.sendMessage("§f/skills introduction [技能]  §a查看技能介绍");
                sender.sendMessage("§e目前支持的技能有：");
                sender.sendMessage("§f剑术:§6 sword");
                sender.sendMessage("§f斧术:§6 axe");
            }
        }
        return false;
    }
}
